<template>
  <div class="login-container">
    <div class="login-box">
      <h2>登录</h2>
      <form @submit.prevent="login">
        <div class="form-group">
          <label for="username">用户名</label>
          <input type="text" v-model="username" id="username" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" v-model="password" id="password" required />
        </div>
        <button type="submit">登录</button>
      </form>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    ...mapMutations(['setUsername']),
    login() {
      if (this.username) {
        this.setUsername(this.username); // 更新 Vuex 中的用户名
        localStorage.setItem('authToken', 'your-token');
        this.$router.push({ name: 'Home' });
      } else {
        alert('请输入用户名');
      }
    }
  }
}
</script>
<style scoped>
body,
html {
  height: 100%;
  margin: 0;
  padding: 0;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #001f3f;
  /* 深蓝色背景 */
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 20px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333333;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #333333;
}

input {
  width: 100%;
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #cccccc;
  box-sizing: border-box;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

button:hover {
  background-color: #66b1ff;
}

@media (max-width: 500px) {
  .login-box {
    padding: 15px;
  }

  button {
    font-size: 14px;
  }
}
</style>
